package com.ruoyi.dynamic.model.config;

import com.ruoyi.dynamic.model.constant.DynamicConstant;
import com.ruoyi.dynamic.model.constant.RoomChatRoomKvConstant;
import com.ruoyi.dynamic.model.pojo.entity.RoomVote;
import com.ruoyi.dynamic.service.ILuckyBagService;
import com.ruoyi.dynamic.service.IRoomAuctionService;
import com.ruoyi.dynamic.service.IRoomVoteMusicService;
import com.ruoyi.dynamic.service.IRoomVoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ExpirationListener {

    @Autowired
    private IRoomVoteService iRoomVoteService;
    @Autowired
    private IRoomVoteMusicService iRoomVoteMusicService;
    @Autowired
    private IRoomAuctionService iRoomAuctionService;
    @Autowired
    private ILuckyBagService luckyBagService;

    // 处理过期键的方法，当有键过期时，Redis会发布一条消息，该方法会被调用
    public void handleExpiredKey(String key) {
        System.out.println("Key expired: " + key);
        /*礼物投票结束*/
        if (key.contains(RoomChatRoomKvConstant.ROOM_VOTE)) {
            String replace = key.replace(RoomChatRoomKvConstant.ROOM_VOTE, "");
            RoomVote roomVote = iRoomVoteService.getById(replace);
            iRoomVoteService.overVote(roomVote, DynamicConstant.CheckStatus.FENG);
        }
        /*礼物投票结束1分钟*/
        if (key.contains(RoomChatRoomKvConstant.ROOM_VOTE_END)) {
            String replace = key.replace(RoomChatRoomKvConstant.ROOM_VOTE_END, "");
            RoomVote roomVote = iRoomVoteService.getById(replace);
            iRoomVoteService.overVote(roomVote, DynamicConstant.CheckStatus.START);
        }
        /*歌曲投票结束*/
        if (key.contains(RoomChatRoomKvConstant.ROOM_AUTION_KEY)) {
            String replace = key.replace(RoomChatRoomKvConstant.ROOM_AUTION_KEY, "");
            iRoomAuctionService.over(Long.valueOf(replace));
        }
        /*歌曲投票结束*/
        if (key.contains(RoomChatRoomKvConstant.ROOM_VOTE_MUSIC)) {
            String replace = key.replace(RoomChatRoomKvConstant.ROOM_VOTE_MUSIC, "");
            //投票场次id
            iRoomVoteMusicService.voteOver(replace, DynamicConstant.CheckStatus.FENG);
        }
        /*歌曲投票1分钟结束*/
        if (key.contains(RoomChatRoomKvConstant.ROOM_VOTE_MUSIC_END)) {
            String replace = key.replace(RoomChatRoomKvConstant.ROOM_VOTE_MUSIC_END, "");
            //投票场次id
            iRoomVoteMusicService.voteOver(replace, DynamicConstant.CheckStatus.START);
        }
        /*福袋倒计时结束*/
        if (key.contains(RoomChatRoomKvConstant.ROOM_LUCK_BAG_ID)) {
            String replace = key.replace(RoomChatRoomKvConstant.ROOM_LUCK_BAG_ID, "");
            luckyBagService.over(replace, DynamicConstant.CheckStatus.FENG);
        }
    }
}
